package com.dy.scs.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dy.scs.pojo.Result;
import com.dy.scs.pojo.SC;
import com.dy.scs.pojo.SCpair;
import com.dy.scs.pojo.gradeVO;
import com.dy.scs.service.SCService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * SC
 */
@RestController
@Slf4j
@RequestMapping("/sc")
public class SCController {
    @Autowired
    private SCService scService;

    /**
     * 分页查询+排序+模糊
     * @param pages
     * @param pageSize
     * @return
     */
    @PostMapping("/page")
    public Result pages(@RequestParam(defaultValue = "1")Integer pages,
                        @RequestParam(defaultValue = "10")Integer pageSize,
                        @RequestParam(required = false) String key,

                        @RequestParam(required = false) String sortBy,
                        @RequestParam(required = false,defaultValue = "asc") String sortType){
        Page<SC> page=scService.pages(pages,pageSize,key,sortBy,sortType);
        return Result.success(page);
    }

    /**
     * 删除所有
     * @param sno
     * @return
     */
    @DeleteMapping("/deleteAll")
    public Result deleteAll(@RequestBody List<SC> sno){
        boolean b = scService.removeBatchByIds(sno);
        if(!b){
            return Result.error("删除失败");
        }
        return Result.success();
    }

    /**
     * 根据sno和cno删除
     * @param sno
     * @param cno
     * @return
     */
    @DeleteMapping("/deleteBy{sno}/{cno}")
    public Result deleteBySnoCno(@PathVariable String sno,@PathVariable String cno){
        QueryWrapper<SC> scQueryWrapper=new QueryWrapper<>();
        scQueryWrapper.eq("sno",sno).eq("cno",cno);
        boolean remove = scService.remove(scQueryWrapper);
        if(!remove){
            return Result.error("删除失败");
        }
        return Result.success();
    }

    /**
     * 修改
      * @param sc
     * @return
     */
    @PutMapping("/update")
    public Result update (@RequestBody SC sc){
        scService.updateById(sc);
        return Result.success();
    }

    /**
     * 插入
     * @param sc
     * @return
     */
    @PostMapping("/insert")
    public Result insert(@RequestBody SC sc){
        boolean save = scService.save(sc);
        if(!save){
            return Result.error("添加失败");
        }
        return Result.success();
    }

    /**
     * 根据sno(多表)
     * @param sno
     * @return
     */
    @GetMapping("/getSC{sno}")
    public Result<List<gradeVO>> getSC(@PathVariable String sno){

        return  scService.getBySC(sno);
    }

    @GetMapping("/getSCBy{cno}")
    public Result<List<gradeVO>> getSCBycno(@PathVariable String cno){

        return scService.getSCBycno(cno);
    }
    /**
     * 根据学号课程号查询成绩
     * @param cno
     * @param sno
     * @return
     */
    @GetMapping("/getSCBy{sno}/{cno}")
    public Result getSCBy(@PathVariable String cno,@PathVariable String sno){
        if(cno==null|| cno.isEmpty()){
            return Result.error("该课程号为空");
        }
        if(sno==null||sno.isEmpty()){
            return Result.error("该学号为空");
        }
        SC sc= scService.getSCBy(cno,sno);
        if(sc==null){
            return Result.error("查询为空");
        }
        return Result.success(sc);
    }

    /**
     * 批量删除
     * @param sCpairs
     * @return
     */
    @DeleteMapping("/deletes")
    public Result deletes(@RequestBody List<SCpair> sCpairs){
        scService.deletes(sCpairs);
        return Result.success();
    }
}

